#include "headfiles.h"

using namespace std;

int main(int argc, char **argv) {
    if (argc != 2) {
        cout << "Usage: " << argv[0] << " matrix" << endl;
        return 0;
    }

    string file_name(argv[1]);
    int row_num, column_num;

    fstream fs;
    fs.open(file_name.c_str());
    if (!fs.is_open()) {
        cout << "File " << file_name << " open error!" << endl;
        return 1;
    }

    string str;
    fs >> str;
    row_num = atoi(str.c_str());
    fs >> str;
    column_num = atoi(str.c_str());

    if (row_num <= 0 ||
        column_num <= 0) {
        cout << "Invalid row or column number!" << endl;
        return 1;
    }

    int *matrix = (int *)malloc(row_num * column_num * sizeof(int));
    if (matrix == NULL) {
        cout << "Allocate matrix buffer error!" << endl;
        return 1;
    }

    int index = 0;
    while (fs >> str) {
        if (str.empty()) {
            break;
        }

        int val = atoi(str.c_str());
        if (val != 0 &&
            val != 1) {
            cout << "Invalid element val!" << endl;
            return 1;
        }

        matrix[index] = val;
        ++index;
    }
    fs.close();

    if (index != row_num * column_num) {
        cout << "Incorrect number of elements for input matrix" << endl;
        return 1;
    }

    bool ret;
    int x_pos, y_pos, size;
    ret = calcMaxSquare(matrix, row_num, column_num, x_pos, y_pos, size);
    cout << "max size = " << size << endl;
    cout << "max pos: (" << x_pos << ", " << y_pos << ")" << endl;

    return 0;
}
